package com.tenton.servlet;

import com.tenton.pojo.Book;
import com.tenton.service.BookService;
import com.tenton.service.Impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date: 2021/1/19
 * @Author: Tenton
 * @Description: 查询所有用户
 */
@WebServlet(name = "ListBookServlet",urlPatterns = "/listBookServlet")
public class ListBookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码格式，防止乱码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        BookService bookService = new BookServiceImpl();
        //获取所有图书信息
        List<Book> books = bookService.listBook();
        //将获取到的图书信息链表保存在request域中，传递给前端
//        ArrayList<Object> list = new ArrayList<>();
//        for (Book book : books) {
//            list.add(book);
//        }
        request.setAttribute("books",books);
        //请求转发到bookManager页面
        request.getRequestDispatcher("/bookManager.jsp").forward(request,response);
    }
}
